package com.ruoyi.equipment.service.impl;

import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.mapper.SysUserMapper;
import org.apache.poi.hssf.record.DVALRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.equipment.mapper.TUserLogMapper;
import com.ruoyi.equipment.domain.TUserLog;
import com.ruoyi.equipment.service.ITUserLogService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 设备日志Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-12-27
 */
@Service
public class TUserLogServiceImpl implements ITUserLogService 
{
    @Autowired
    private TUserLogMapper tUserLogMapper;

    @Autowired
    private SysUserMapper  userMapper;

    /**
     * 查询设备日志
     * 
     * @param
     * @return 设备日志
     */
    @Override
    public TUserLog selectTUserLogByUserId(Integer userLogId)
    {
        return tUserLogMapper.selectTUserLogByUserId(userLogId);
    }

    /**
     * 查询设备日志列表
     * 
     * @param tUserLog 设备日志
     * @return 设备日志
     */
    @Override
    @DataScope(deptAlias = "d",userAlias = "f")
    public List<TUserLog> selectTUserLogList(TUserLog tUserLog)
    {
        return tUserLogMapper.selectTUserLogList(tUserLog);
    }

    /**
     * 新增设备日志
     * 
     * @param tUserLog 设备日志
     * @return 结果
     */
    @Override
    public int insertTUserLog(TUserLog tUserLog)
    {
        return tUserLogMapper.insertTUserLog(tUserLog);
    }

    /**
     * 修改设备日志
     * 
     * @param tUserLog 设备日志
     * @return 结果
     */
    @Override
    public int updateTUserLog(TUserLog tUserLog)
    {
        return tUserLogMapper.updateTUserLog(tUserLog);
    }

    /**
     * 批量删除设备日志
     * 
     * @param userIds 需要删除的设备日志主键
     * @return 结果
     */
    @Override
    public int deleteTUserLogByUserIds(Integer[] userIds)
    {
        return tUserLogMapper.deleteTUserLogByUserIds(userIds);
    }

    /**
     * 删除设备日志信息
     * 
     * @param userId 设备日志主键
     * @return 结果
     */
    @Override
    public int deleteTUserLogByUserId(Integer userId)
    {
        return tUserLogMapper.deleteTUserLogByUserId(userId);
    }


    //设备归还信息
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String returnDrawer(TUserLog tUserLogVo) {
        TUserLog tUserLog = tUserLogMapper.selectTUserLogByUserId(tUserLogVo.getUserLogId());
        if (tUserLog.getDrawerStu()!=0){
            throw new RuntimeException("仓匣未归还到设备上");
        }
        tUserLogVo.setReturnStu(0);
        tUserLogMapper.updateTUserLog(tUserLogVo);
        return "归还成功";
    }

    //根据仓匣id 获取所有的日志
    @Override
    public SysUser getUserLogByDrawerId(String drawerId) {
        SysUser sysUser = null;
        try {
            Integer userId=   tUserLogMapper.getUserIdByDrawerId(drawerId);
            sysUser = userMapper.selectUserById(userId.longValue());
            return sysUser;
        } catch (Exception e) {
            SysUser sysUser1 = new SysUser();
            return sysUser1;
        }
    }
}
